﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileEncription
{
    public class Encryption
    {
        public static string Encrypt(string fileName) 
        {
            FileStream inputFile = new FileStream(fileName, FileMode.Open);
            using (inputFile) 
            {
                byte[] buffer = new byte[5];
                int read = inputFile.Read(buffer,0,5);
                FileStream outputFile = new FileStream("encrypted" + fileName, FileMode.Create);
                using (outputFile)
                {
                    while (read != 0) 
                    {
                        outputFile.WriteByte(0);
                        outputFile.Write(buffer, 0, read - 1);
                        outputFile.Seek(-(read ), SeekOrigin.Current);
                        outputFile.WriteByte(buffer[read - 1]);
                        outputFile.Seek((read -1), SeekOrigin.Current);
                        read = inputFile.Read(buffer, 0, 5);
                    }   
                }
            }
            return "encrypted" + fileName;
        }

        public static void Decrypt(string fileName) 
        {
            FileStream inputFile = new FileStream(fileName, FileMode.Open);
            using (inputFile)
            {
                byte[] buffer = new byte[5];
                int read = inputFile.Read(buffer,0,5);
                FileStream outputFile = new FileStream("Decrypted" , FileMode.Create);
                using (outputFile)
                {
                    while (read != 0)
                    {
                        outputFile.Write(buffer, 1, read-1);
                        outputFile.WriteByte(buffer[0]);
                        read = inputFile.Read(buffer, 0, 5);
                    }
                }
            }
        }
    }
}
